package com.example.poemtest.mine;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.text.method.TransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.poemtest.R;
import com.example.poemtest.utils.FullScreenUtil;

/**
 * 修改密码页面
 */
public class ChangePassword extends AppCompatActivity {

    private EditText editOldPsd, editNewPsd, editRePsd;  //旧密码，新密码，确认密码
    private ImageView newEye, reEye,imgBack; //新密码眼睛，确认密码眼睛
    private boolean isHide = false;  //输入框密码是否是隐藏，默认为false
    private TextView btnEdit;  //立即修改按钮
    private String oldPsd, newPsd, rePsd;
    private ImageView back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FullScreenUtil.getInstance().fullScreen(this,true);
        setContentView(R.layout.activity_change_password);
        //绑定控件对象
        setView();
        //设置点击事件
        newEye.setOnClickListener(view->{
            isShowPassword(editNewPsd, newEye);
        });
        reEye.setOnClickListener(view->{
            isShowPassword(editRePsd, reEye);
        });
        btnEdit.setOnClickListener(view->{
            if (editNewPsd.getText().toString().equals(editRePsd.getText().toString())){
                Toast.makeText(this,"您已成功修改密码！",Toast.LENGTH_LONG).show();
            }else {
                Toast.makeText(this,"两次输入的密码不一致",Toast.LENGTH_LONG).show();
            }
        });
        imgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ChangePassword.this,SetupSafe.class);
                startActivity(intent);
            }
        });
    }

    /**
     * 点击眼睛图标显示密码明文或密文
     */
    private void isShowPassword(EditText editText, ImageView imageView) {
        if(isHide == false) {
            //R.mipmap.password_show是表示显示密码的“眼睛”图标
            imageView.setImageResource(R.mipmap.eye_open);
            //密文
            HideReturnsTransformationMethod method1 = HideReturnsTransformationMethod.getInstance();
            editText.setTransformationMethod(method1);
            isHide = true;
        } else {
            //R.mipmap.password_miss是表示隐藏密码的“眼睛+斜杠”图标
            imageView.setImageResource(R.mipmap.eye_close);
            //密文
            TransformationMethod method2 = PasswordTransformationMethod.getInstance();
            editText.setTransformationMethod(method2);
            isHide = false;
        }
        //重置光标位置
        int index = editText.getText().toString().length();
        editText.setSelection(index) ;
    }

    /**
     * 获取用户输入的数据
     */
    private void getData() {
        oldPsd = editOldPsd.getText().toString();
        newPsd = editNewPsd.getText().toString();
        rePsd = editRePsd.getText().toString();
    }

    /**
     * 绑定控件对象
     */
    private void setView() {
        editOldPsd = findViewById(R.id.edit_old_psd);
        editNewPsd = findViewById(R.id.edit_new_psd);
        editRePsd = findViewById(R.id.edit_re_psd);
        newEye = findViewById(R.id.new_eye);
        reEye = findViewById(R.id.re_eye);
        btnEdit = findViewById(R.id.btn_edit);
        imgBack = findViewById(R.id.setup_safe_to_mine);
    }

}